<template>
	<div>
		<el-row>
			<el-col :span="24">
                <el-alert v-if="successalert"
                    title="成功提示的文案"
                    type="success"
                    center
                    show-icon>
                </el-alert>
				
				<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
					<el-form-item label="姓名" prop="uname">
						<el-input v-model="ruleForm.uname"></el-input>
					</el-form-item>

					<el-form-item label="手机" prop="uphone">
						<el-input v-model="ruleForm.uphone"></el-input>
					</el-form-item>

					<el-form-item label="城市" prop="city">
						<el-input v-model="ruleForm.city"></el-input>
					</el-form-item>

					<el-form-item label="代理产品" prop="product">
						<el-input v-model="ruleForm.product"></el-input>
					</el-form-item>

					<el-form-item label="备注" prop="remark">
						<el-input v-model="ruleForm.remark"></el-input>
					</el-form-item>

					<el-form-item>
						<el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
						<el-button @click="resetForm('ruleForm')">重置</el-button>
					</el-form-item>
				</el-form>
			</el-col>
		</el-row>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				successalert : false,
				ruleForm: {
					uname: '',
					uphone: '',
					city: '',
					product: '',
					remark: ''
				},
				rules: {
					uname: [{
							required: true,
							message: '请输入姓名',
							trigger: 'blur'
						},
						{
							min: 1,
							max: 10,
							message: '长度在 3 到 5 个字符',
							trigger: 'blur'
						}
					],
					uphone: [{
							required: true,
							message: '请输入联系方式',
							trigger: 'blur'
						},
						{
							min: 1,
							max: 16,
							message: '长度在 3 到 5 个字符',
							trigger: 'blur'
						}
					],
					city: [{
							required: true,
							message: '请输入城市',
							trigger: 'blur'
						},
						{
							min: 1,
							max: 15,
							message: '长度在 3 到 5 个字符',
							trigger: 'blur'
						}
					],
					product: [{
							required: true,
							message: '请输入代理产品',
							trigger: 'blur'
						},
						{
							min: 1,
							max: 15,
							message: '长度在 3 到 5 个字符',
							trigger: 'blur'
						}
					],
					remark: [{
							required: true,
							message: '请输入备注',
							trigger: 'blur'
						},
						{
							min: 1,
							max: 15,
							message: '长度在 3 到 5 个字符',
							trigger: 'blur'
						}
					],
				}
			};
		},
		methods: {
			submitForm(formName) {
				var _this = this;
				this.$refs[formName].validate((valid) => {
					if (valid) {
						// alert('submit!');
						console.log(this.ruleForm)
						// /autoagent_new?uname=asdf&uphone=asdf&company=asdf&companynums=12&city=asdf&product=asdf&remark=asdf
						$.ajax({
							url: this.url + '/autoagent_new',
							data: this.ruleForm,
							success: (data) => {
								console.log(data);
								// this.$router.push('autoagents_details');
								if(data.code == 200){
									// alert(data.msgs);
									_this.successalert = true;
									setTimeout(()=>{
										_this.successalert = false;
									},2000);
								}
								_this.resetForm('ruleForm');
							},
							error() {
								this.$message.error('服务器错误！');
							}
						});
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>


<style scoped>
	.form1 {
		margin-top: 20px;
	}

	.input1 {
		/* width: 200px; */
	}
</style>
